package com.ruoyi.wjq.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.TimeConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wjq.mapper.AnswerRecordMapper;
import com.ruoyi.wjq.domain.AnswerRecord;
import com.ruoyi.wjq.service.IAnswerRecordService;

/**
 * 答题记录Service业务层处理
 * 
 * @author wjq
 * @date 2025-08-13
 */
@Service
public class AnswerRecordServiceImpl implements IAnswerRecordService 
{
    @Autowired
    private AnswerRecordMapper answerRecordMapper;

    /**
     * 查询答题记录
     * 
     * @param id 答题记录主键
     * @return 答题记录
     */
    @Override
    public AnswerRecord selectAnswerRecordById(Long id)
    {
        return answerRecordMapper.selectAnswerRecordById(id);
    }

    /**
     * 查询答题记录列表
     * 
     * @param answerRecord 答题记录
     * @return 答题记录
     */
    @Override
    public List<AnswerRecord> selectAnswerRecordList(AnswerRecord answerRecord){

        // 1. 查询原始数据（包含秒数形式的timeUsed）
        List<AnswerRecord> recordList = answerRecordMapper.selectAnswerRecordList(answerRecord);

        // 2. 遍历结果，使用TimeConverter转换timeUsed格式
        for (AnswerRecord record : recordList) {
            // 获取秒数（确保数据库中timeUsed存储的是总秒数）
            Long totalSeconds = record.getTimeUsed();
            // 转换为"X小时X分X秒"格式的字符串
            String timeStr = TimeConverter.toMinSecStr(totalSeconds);
            // 将转换后的值设置回对象（如果需要在前端展示，可新增String类型字段存储）
            record.setTimeUsedStr(timeStr);
        }

        return recordList;
    }
    
    @Override
    public List<AnswerRecord> selectAnswerRecordWithBankList(AnswerRecord answerRecord){
        
        // 1. 查询关联数据（包含秒数形式的timeUsed和题库信息）
        List<AnswerRecord> recordList = answerRecordMapper.selectAnswerRecordWithBankList(answerRecord);
        
        // 2. 遍历结果，使用TimeConverter转换timeUsed格式
        for (AnswerRecord record : recordList) {
            // 获取秒数
            Long totalSeconds = record.getTimeUsed();
            // 转换为"X小时X分X秒"格式的字符串
            String timeStr = TimeConverter.toMinSecStr(totalSeconds);
            // 将转换后的值设置回对象
            record.setTimeUsedStr(timeStr);
        }
        
        return recordList;
    }

    /**
     * 新增答题记录
     * 
     * @param answerRecord 答题记录
     * @return 结果
     */
    @Override
    public int insertAnswerRecord(AnswerRecord answerRecord)
    {
        answerRecord.setCreateTime(DateUtils.getNowDate());
        return answerRecordMapper.insertAnswerRecord(answerRecord);
    }

    /**
     * 修改答题记录
     * 
     * @param answerRecord 答题记录
     * @return 结果
     */
    @Override
    public int updateAnswerRecord(AnswerRecord answerRecord)
    {
        return answerRecordMapper.updateAnswerRecord(answerRecord);
    }

    /**
     * 批量删除答题记录
     * 
     * @param ids 需要删除的答题记录主键
     * @return 结果
     */
    @Override
    public int deleteAnswerRecordByIds(Long[] ids)
    {
        return answerRecordMapper.deleteAnswerRecordByIds(ids);
    }

    /**
     * 删除答题记录信息
     * 
     * @param id 答题记录主键
     * @return 结果
     */
    @Override
    public int deleteAnswerRecordById(Long id)
    {
        return answerRecordMapper.deleteAnswerRecordById(id);
    }
}
